﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetServer
{
    public class SendDatas
    {
        /// <summary>
        /// 数据处理
        /// </summary>
        /// <param name="RcvData"></param>
        /// <returns></returns>
        public static byte[] SendData(string IpAndPort, string RcvData)
        {
            //功能号/序列号/包内容
            string func_no, serial_id, body;
            byte[] byteArray;
            func_no = RcvData.Substring(4, 2);
            serial_id = RcvData.Substring(6, 4);
            body = RcvData.Substring(10, RcvData.Length - 10);
            //Server端的当前时间
            string time = DateTime.Now.ToString("yyMMddHHmmss");

            if (func_no.Equals("10"))
            {
                //终端机认证
                int ret = Authentication(IpAndPort, body);//认证状态（做查询操作）
                string des_txt = MyLogger.formatTxt(MyLogger.CARD_KEY, 12) + MyLogger.getRandom(4) + MyLogger.formatTxt(MyLogger.CARD_DES, 16);
                string str = ret + time + MyLogger.formatInt(MyLogger.CARD_BLOCK, 2) + des_txt;
                //  string str = ret + time;
                for (int i = 1; i <= 20; ++i)
                {
                    string key = MyLogger.formatInt(i, 2);
                    string des = null;
                    try
                    {
                        des = (string)MyLogger.codeMap[key];
                    }
                    catch (Exception)
                    {
                    }
                    if (des == null)
                        des = "";
                    str = str + key + MyLogger.formatTxt(des, 32);
                }
                byteArray = sendData(func_no, serial_id, str);
                //加密密钥
                byte[] des_data = MyDes.doDesByte(des_txt, "E3F2D6DDBAEABEA2");
                if ((des_data != null) && (des_data.Length == 32))
                {
                    Array.Copy(des_data, 0, byteArray, 10, des_data.Length);
                }
            }
            else if (func_no.Equals("05"))
            {
                //心跳包,网络连接状态查询
                byteArray = sendData(func_no, serial_id, time);
            }
            else if (func_no.Equals("96"))
            {
                //刷卡消费
                byteArray = saveAttend(func_no, serial_id, body);
            }
            else if (func_no.Equals("97"))
            {
                //退款请求
                byteArray = sendData(func_no, serial_id, time);
            }
            else if (func_no.Equals("91"))
            {
                //操作员登陆
                byteArray = sendData(func_no, serial_id, time);
            }
            else
            {
                byteArray = sendData(func_no, serial_id, time);
            }

            return byteArray;
        }
        /// <summary>
        /// 普通验证和心跳包
        /// </summary>
        /// <param name="func_no"></param>
        /// <param name="serial_id"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        private static byte[] sendData(string func_no, string serial_id, string body)
        {
            if (serial_id.Length != 4)
                serial_id = MyLogger.formatTxt(serial_id, 4, "0", false);
            if (func_no.Length != 2)
            {
                func_no = MyLogger.formatTxt(func_no, 2);
            }
            byte[] body_data = ByteUtil.getBytes(func_no + serial_id + body);
            int iLen = 4 + body_data.Length;
            if (iLen > 9999)
                return null;
            string head = MyLogger.formatInt(iLen, 4);
            byte[] leng = ByteUtil.getBytes(head);
            body_data = leng.Concat(body_data).ToArray();
            return body_data;
        }
        /// <summary>
        /// 刷卡消费
        /// </summary>
        /// <param name="func_no"></param>
        /// <param name="serial_id"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        private static byte[] saveAttend(string func_no, string serial_id, string body)
        {
            //AFF00005 00003673 01 00000000     
            int pos = 0;
            //收费机ID
            string deviceId = "";
            if (body.Length >= pos + 8)
                deviceId = body.Substring(pos, 8);
            pos += 8;
            //卡片ID号
            string cardId = "";
            if (body.Length >= pos + 8)
                cardId = body.Substring(pos, 8);
            pos += 8;
            //消费类别
            string swingType = "";
            if (body.Length >= pos + 2)
                swingType = body.Substring(pos, 2);
            pos += 2;
            //消费数额(Money)
            string swingAmount = "";
            if (body.Length >= pos + 8)
            {
                swingAmount = body.Substring(pos, 8);
            }
            int expense = 0;
            if (swingAmount.Length > 0)
                expense = (int)Double.Parse(swingAmount);
            pos += 8;
            //操作员ID
            string operatorId = "";
            if (body.Length >= pos + 2)
            {
                operatorId = body.Substring(pos, 2);
            }
            return GetsaveAttend(func_no, serial_id, deviceId, cardId, swingType, expense, operatorId);
        }
        /// <summary>
        /// 后台操作
        /// </summary>
        /// <param name="deviceId">收费机ID</param>
        /// <param name="cardId">卡片ID号</param>
        /// <param name="swingType">消费类别</param>
        /// <param name="expense">消费数额(Money)</param>
        /// <param name="operatorId">操作员ID</param>
        /// <returns></returns>
        private static byte[] GetsaveAttend(string func_no, string serial_id, string deviceId, string cardId, string swingType, int expense, string operatorId)
        {
            string body = "";
            //字符提示信息
            string NoticString = "00";
            //提示音类别
            string NoticSound = "01";
            //余额(分)--100元
            int Balance = 10000;
            //姓名
            string Name = "张三";
            body = cardId + NoticString + NoticSound + swingType + MyLogger.formatInt(expense, 8) + MyLogger.formatInt(Balance, 8) + MyLogger.formatTxt(Name, 8);
            int Length = System.Text.Encoding.Default.GetByteCount(Name);
            return sendData(func_no, serial_id, body); ;
        }

        private static int Authentication(string IpAndPort, string body)
        {
            //收费机ID
            string DeviceId = body.Substring(0, 8);
            string ip = IpAndPort.Split(':')[0];

            return 1;
        }
    }
}
